package activiti;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;

import java.util.List;

public class FifthTest {

    public static void main(String[] args) {

        ProcessEngineConfiguration pec = ProcessEngineConfiguration.createProcessEngineConfigurationFromResourceDefault();

        ProcessEngine pe = pec.buildProcessEngine();

        IdentityService is = pe.getIdentityService();

        //创建用户组名Student
        Group studentGroup = is.newGroup("student");
        //为用户组名赋值
        studentGroup.setId("s01");
        studentGroup.setName("Student");
        studentGroup.setType("Assignment01");
        //保存一下
        is.saveGroup(studentGroup);

        //创建用户组名Teacher
        Group teacherGroup = is.newGroup("teacher");
        //为用户组名赋值
        teacherGroup.setId("t01");
        teacherGroup.setName("Teacher");
        teacherGroup.setType("Assignment02");
        //保存一下
        is.saveGroup(teacherGroup);

        //创建用户tom
        User tom = is.newUser("tom");
        tom.setEmail("tomcat@pdsu.edu.cn");
        tom.setId("151210217");
        tom.setFirstName("tom");
        tom.setLastName("bear");
        is.setUserInfo(tom.getId(),"Age","18");
        is.setUserInfo(tom.getId(),"Sex","boy");
        is.saveUser(tom);

        //创建用户jack
        User jack = is.newUser("jack");
        jack.setEmail("javajack@pdsu.edu.cn");
        jack.setId("171210216");
        jack.setFirstName("jack");
        jack.setLastName("beautiful");
        is.setUserInfo(jack.getId(),"Age","18");
        is.setUserInfo(jack.getId(),"Sex","girl");
        is.saveUser(jack);

        //绑定用户与组
        //tom的
        is.createMembership(tom.getId(),teacherGroup.getId());
        is.createMembership(tom.getId(),studentGroup.getId());
        //jack的
        is.createMembership(jack.getId(),teacherGroup.getId());

        //		查询id为t01的用户组
        System.out.println("查询id为t01的用户组：");
        Group group1=is.createGroupQuery().groupId("t01").singleResult();
        System.out.println("GroupID:"+group1.getId()+",   Group Name:"+group1.getName()+",   Group Type:"+group1.getType());

//		通过用户组类型查询并降序输出
        System.out.println("通过用户组类型降序输出：");
        List<Group> groups=is.createGroupQuery().orderByGroupType().desc().list();//结果降序    asc()  结果升序
        for(Group test:groups) {
            System.out.println("GroupID:"+test.getId()+",   Group Name:"+test.getName()+",   Group Type:"+test.getType());
        }

        //User queryUser=is.createUserQuery().userFirstName("tom").singleResult(); 查询返回结果必为单个
        List<User> queryUser=is.createUserQuery().userFirstName("tom").list();//list返回多个
        System.out.println("查询名为tom的用户：");
        for(User user:queryUser) {
            System.out.println("UserID:"+user.getId()+",UserFirstName:"+user.getFirstName()+",UserLastName:"+
                    user.getLastName()+",UserEmali:"+user.getEmail()+",UserAge:"+is.getUserInfo(user.getId(), "Age")+",UserSex:"+
                    is.getUserInfo(user.getId(), "Sex"));
        }

        List<User> queryUsers=is.createUserQuery().userLastName("bear").list();
        System.out.println("查询姓为bear的用户：");
        for(User user:queryUsers) {
            System.out.println("UserID:"+user.getId()+",UserFirstName:"+user.getFirstName()+
                    ",UserLastName:"+user.getLastName()+",UserEmali:"+user.getEmail()+",UserAge:"
                    +is.getUserInfo(user.getId(), "Age")+",UserSex:"+is.getUserInfo(user.getId(), "Sex"));
        }


        List<User> users=is.createUserQuery().memberOfGroup(teacherGroup.getId()).list();
        System.out.println("查询teacher包含那些用户：");
        for(User query:users) {
            System.out.println("UserFirstName:"+query.getFirstName());

        }

        List<Group> groupss=is.createGroupQuery().groupMember(tom.getId()).list();
        System.out.println("查询tom包含那些用户组：");
        for(Group group:groupss) {
            System.out.println("GroupName:"+group.getName());

        }
    }

}




//        //创建任务查询
//        List<Group> groupList =is.createGroupQuery().list();
//
//        //遍历集合 迭代
//        for (Group g:groupList) {
//            System.out.println("GroupId:"+g.getId()+"   GroupName:"+g.getName()+"   GroupType"+g.getType());
//        }

        //如果只有一个用户组
        //创建用户组名Teacher
//        Group testGroup = is.newGroup("test");
//        //为用户组名赋值
//        testGroup.setId("test01");
//        testGroup.setName("test");
//        testGroup.setType("test01");
//        //保存一下
//        is.saveGroup(testGroup);
//        //这次只有一条数据，可以不用List接收
//        Group result = is.createGroupQuery().singleResult();
//        System.out.println("GroupId:"+result.getId()+"   GroupName:"+result.getName()+"   GroupType"+result.getType());

